package com.linkwechat.framework.config;



import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import com.github.pagehelper.PageInterceptor;
import com.linkwechat.common.tenant.TenantContextHolder;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Mybatis支持*匹配扫描包
 * 
 * @author ruoyi
 */
@Configuration
public class MyBatisPlusConfig
{


    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//        CustomerPaginationInterceptor paginationInterceptor = new CustomerPaginationInterceptor();
        //多租户配置...
        // 创建SQL解析器集合
        List<ISqlParser> sqlParserList = new ArrayList<>();

        // 创建租户SQL解析器
        TenantSqlParser tenantSqlParser = new TenantSqlParser();

        // 设置租户处理器
        tenantSqlParser.setTenantHandler(new TenantHandler() {
            @Override
            public Expression getTenantId(boolean select) {
                // 设置当前租户ID，实际情况你可以从cookie、或者缓存中拿都行
//                return new StringValue("0");
                String tenantCode=TenantContextHolder.getTenantCode();
                // 启动时读取sys_config的配置，默认为0
                if(tenantCode==null){
                    return new StringValue("0");
                }
                return new StringValue(tenantCode);
            }

            @Override
            public String getTenantIdColumn() {
                // 对应数据库租户ID的列名
//                return "tenant_id";
                return "tenant_code";
            }

            @Override
            public boolean doTableFilter(String tableName) {
                // 是否需要需要忽略某一张表
                List<String> tableNameList = new ArrayList<>(Arrays.asList(
                        "sys_menu","sys_role_menu","sys_user_role","sys_role","sys_dict_type","sys_dict_data" // 公共功能相关表
                        ,"tables","columns","character_sets")); // 数据库的库的数据字典表
                String tenantCode=TenantContextHolder.getTenantCode();
                // 启动时读取sys_config的配置，默认为0
                if(tenantCode==null){
                    tenantCode="0";
                }
                if(tenantCode.equals("0")){
                    tableNameList.add("sys_user");
                    tableNameList.add("sys_tenant");
                    tableNameList.add("sys_role");
                    tableNameList.add("sys_user_role");
                }

                if (tableNameList.contains(tableName)){
                    return true;
                }

                return false;
            }
        });

        sqlParserList.add(tenantSqlParser);
        paginationInterceptor.setSqlParserList(sqlParserList);

        return paginationInterceptor;

    }

    //pagehelper 分页配置
    @Bean
    ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return new ConfigurationCustomizer() {
            @Override
            public void customize(MybatisConfiguration configuration) {
                configuration.addInterceptor(new com.github.pagehelper.PageInterceptor());
            }
        };
    }





}